package com.bawei.mall.user.controller;

import com.bawei.common.core.domain.R;
import com.bawei.common.core.utils.PageUtils;
import com.bawei.mall.user.domain.*;
import com.bawei.mall.user.service.IMallProductOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：HaoYunde
 * @Package：com.bawei.mall.user.controller
 * @Project：mall_cloud
 * @name：MallProductorderController
 * @Date：2024/7/9 11:17
 */
@RestController
@RequestMapping("/order")
public class MallProductOrderController {
    @Autowired
    private IMallProductOrderService productOrderService;


    /**
     * 项目初始化启动调用
     */
    @PostConstruct
    public void ss(){
        System.out.println("第一种方法");
    }



    /**
     * 订单确认
     * @param ids
     * @return
     */
    @GetMapping("generateConfirmOrder")
    public R<MallProductOrder> generateConfirmOrder(@RequestParam List<String> ids){
        return  productOrderService.generateConfirmOrder(ids);
    }


    /**
     * 提交订单
     * @param orderInfo
     * @return
     */
    @PostMapping("/generateOrder")
    public R<MallCusOrderInfo> generateOrder(@RequestBody MallCusOrderInfo orderInfo){
        return productOrderService.generateOrder(orderInfo);
    }

    /**
     * 提交订单的中间表
     * @param orderId,list
     * @return
     */
    @PostMapping("/generateOrderMiddle")
    public R<MallCusProductOrderMiddle> generateOrderMiddle( @RequestParam Long orderId,@RequestBody List<MallProductEntity> list){
        List<MallCusProductOrderMiddle> mallCusProductOrderMiddles = new ArrayList<>();
        MallCusProductOrderMiddle mallCusProductOrderMiddle = new MallCusProductOrderMiddle();
        for (MallProductEntity mallProduct : list) {
            mallCusProductOrderMiddle.setSkuId(mallProduct.getProductSkuId());
            mallCusProductOrderMiddle.setQuantity(mallProduct.getQuantity());
            mallCusProductOrderMiddle.setPrice(mallProduct.getPrice());
            mallCusProductOrderMiddle.setProductTitle(mallProduct.getProductSubTitle());
            mallCusProductOrderMiddle.setPic(mallProduct.getProductPic());
            mallCusProductOrderMiddles.add(mallCusProductOrderMiddle);
        }

        productOrderService.generateOrderMiddle(orderId,mallCusProductOrderMiddles);
         return R.ok();
    }



    /**
     * 订单列表
     */
    @GetMapping("/list")
    public R<List<MallOrderList>> list(@RequestParam Integer payStatus){
        List<MallOrderList> list=productOrderService.list(payStatus);
        return R.ok(list);
    }





}
